package com.k.liuzh.extraffic.Fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.k.liuzh.extraffic.Database.QueryTraffucLightBean;
import com.k.liuzh.extraffic.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

public class HLDeng_Fragment extends Fragment {
    private static final String TAG = "HLDeng_Fragment";
    @Nullable
    private ListView mListview;
    private Spinner mSpinner;
    private Button mButton;
    private RequestQueue requestQueue;
    private List<QueryTraffucLightBean> list;
    private List<QueryTraffucLightBean> orderByRed;
    private List<QueryTraffucLightBean> orderByGreen;
    private List<QueryTraffucLightBean> orderByYellow;
    private List<QueryTraffucLightBean> orderByCross;
    private Timer timer;
    private MyAdapter adapter;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_hldeng,container,false);
        mButton = view.findViewById(R.id.btn_hldeng_query);
        mSpinner = view.findViewById(R.id.spn_hldeng);
        mListview = view.findViewById(R.id.listview_hldeng);
        list = new ArrayList<>();
        requestQueue = Volley.newRequestQueue(getContext());
        for (int i = 1; i < 8; i++){
            initData(i);
        }



        //setListData();
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d(TAG, "onClick: This is "+mSpinner.getSelectedItem().toString());
                mListview.setAdapter(new MyAdapter(getContext(),list));
                adapter = null;
                String query = mSpinner.getSelectedItem().toString();
                switch (query){
                    case "路口升序":
                        adapter = new MyAdapter(getContext(),upCross());
                        break;
                    case "路口降序":
                        adapter = new MyAdapter(getContext(),downCross());
                        break;
                    case "红灯降序":
                        adapter = new MyAdapter(getContext(),downRed());
                        break;
                    case "红灯升序":
                        adapter = new MyAdapter(getContext(),upRed());
                        break;
                    case "绿灯降序":
                        adapter = new MyAdapter(getContext(),downGreen());
                        break;
                    case "绿灯升序":
                        adapter = new MyAdapter(getContext(),upGreen());
                        break;
                    case "黄灯降序":
                        adapter = new MyAdapter(getContext(),downYellow());
                        break;
                    case "黄灯升序":
                        adapter = new MyAdapter(getContext(),upYellow());
                        break;

                }
                mListview.setAdapter(adapter);

                for(QueryTraffucLightBean be : orderByCross){
                    Log.d(TAG, "onClick: "+orderByCross.size());
                    Log.d(TAG, "onClick: "+be);
                }
            }
        });
        return view;
    }
/*
    private void setListData(){
        if(list.size() == 0){
            this.setListData();
        }else {
            mListview.setAdapter(new MyAdapter(getContext(),list));
            orderByCross = list;
            orderByGreen = list;
            orderByYellow = list;
            orderByRed = list;
        }
    }
*/
    private List<QueryTraffucLightBean> downCross(){
        orderByCross = list;
        Collections.sort(orderByCross, new Comparator<QueryTraffucLightBean>() {
            @Override
            public int compare(QueryTraffucLightBean q1, QueryTraffucLightBean q2) {
                int i = -(q1.getCross() - q2.getCross());
                return i;
            }
        });
        return orderByCross;
    }
    private List<QueryTraffucLightBean> downYellow(){
        orderByYellow = list;
        Collections.sort(orderByYellow, new Comparator<QueryTraffucLightBean>() {
            @Override
            public int compare(QueryTraffucLightBean q1, QueryTraffucLightBean q2) {
                int i = -(q1.getYellow() - q2.getYellow());
                return i;
            }
        });
        return orderByYellow;
    }
    private List<QueryTraffucLightBean> downRed(){
        orderByRed = list;
        Collections.sort(orderByRed, new Comparator<QueryTraffucLightBean>() {
            @Override
            public int compare(QueryTraffucLightBean q1, QueryTraffucLightBean q2) {
                int i = -(q1.getRed() - q2.getRed());
                return i;
            }
        });
        return orderByRed;
    }
    private List<QueryTraffucLightBean> downGreen(){
        orderByGreen = list;
        Collections.sort(orderByGreen, new Comparator<QueryTraffucLightBean>() {
            @Override
            public int compare(QueryTraffucLightBean q1, QueryTraffucLightBean q2) {
                int i = -(q1.getGreen() - q2.getGreen());
                return i;
            }
        });
        return orderByGreen;
    }
    private List<QueryTraffucLightBean> upCross(){
        orderByCross = list;
        Collections.sort(orderByCross, new Comparator<QueryTraffucLightBean>() {
            @Override
            public int compare(QueryTraffucLightBean q1, QueryTraffucLightBean q2) {
                int i = q1.getCross() - q2.getCross();
                return i;
            }
        });
        return orderByCross;
    }
    private List<QueryTraffucLightBean> upRed(){
        orderByRed = list;
        Collections.sort(orderByRed, new Comparator<QueryTraffucLightBean>() {
            @Override
            public int compare(QueryTraffucLightBean q1, QueryTraffucLightBean q2) {
                int i = q1.getRed() - q2.getRed();
                return i;
            }
        });
        return orderByRed;
    }
    private List<QueryTraffucLightBean> upYellow(){
        orderByYellow = list;
        Collections.sort(orderByYellow, new Comparator<QueryTraffucLightBean>() {
            @Override
            public int compare(QueryTraffucLightBean q1, QueryTraffucLightBean q2) {
                int i = q1.getYellow() - q2.getYellow();
                return i;
            }
        });
        return orderByYellow;
    }

    private List<QueryTraffucLightBean> upGreen(){
        orderByGreen = list;
        Collections.sort(orderByGreen, new Comparator<QueryTraffucLightBean>() {
            @Override
            public int compare(QueryTraffucLightBean q1, QueryTraffucLightBean q2) {
                int i = q1.getGreen() - q2.getGreen();
                return i;
            }
        });
        return orderByGreen;
    }

    /**
     * 查询id路口的红绿灯设置信息
     * @param id
     */
    private void initData(int id) {
        final int crossId = id;
        Bundle bundle = this.getArguments();
        String name = bundle.getString("userName");
        String url = "http://47.106.153.72:8081/transportservice/action/GetTrafficLightConfigAction.do";
        JSONObject js = new JSONObject();
        try {
            js.put("TrafficLightId",crossId);
            js.put("UserName",name);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, js, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Gson gson = new Gson();
                try {
                    int red = jsonObject.getInt("RedTime");
                    int green = jsonObject.getInt("GreenTime");
                    int yellow = jsonObject.getInt("YellowTime");
                    QueryTraffucLightBean bean = new QueryTraffucLightBean(crossId,red,yellow,green);

                    Log.d(TAG, "onResponse: "+jsonObject);
                    list.add(bean);
                    if(list.size() == 7){
                        adapter = new MyAdapter(getContext(),upCross());
                        mListview.setAdapter(adapter);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(getContext(),"网络错误",Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);
    }


    private class MyAdapter extends BaseAdapter{

        private Context context;
        private List<QueryTraffucLightBean> list;

        public MyAdapter(Context context, List<QueryTraffucLightBean> list) {
            this.context = context;
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder viewHolder = null;
            LayoutInflater inflater = LayoutInflater.from(context);
            if(viewHolder == null){
                viewHolder = new ViewHolder();
                view = inflater.inflate(R.layout.listview_hldeng,null);
                viewHolder.textViewCross = view.findViewById(R.id.textview_hldeng_cross);
                viewHolder.textViewRed = view.findViewById(R.id.textview_hldeng_red);
                viewHolder.textViewGreen = view.findViewById(R.id.textview_hldeng_green);
                viewHolder.textViewYellow = view.findViewById(R.id.textview_hldeng_yellow);
                view.setTag(viewHolder);
            }else {
                viewHolder = (ViewHolder) view.getTag();
            }
            QueryTraffucLightBean bean = list.get(i);
            viewHolder.textViewCross.setText(bean.getCross()+"");
            viewHolder.textViewRed.setText(bean.getRed()+"");
            viewHolder.textViewGreen.setText(bean.getGreen()+"");
            viewHolder.textViewYellow.setText(bean.getGreen()+"");
            return view;
        }

        class ViewHolder{
            TextView textViewCross;
            TextView textViewRed;
            TextView textViewYellow;
            TextView textViewGreen;
        }
    }
}
